from zope.interface import Interface
from zope.interface import implements
from Products.CMFCore.utils import getToolByName
#from zope.component import getUtility
#from zope.component import getMultiAdapter
from Acquisition import aq_inner
from plone.memoize.instance import memoize

from plone.app.portlets.portlets import base
from plone.portlets.interfaces import IPortletDataProvider

from zope import schema
from zope.formlib import form
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile

from my315ok.fangcan import fangcanMessageFactory as _
from my315ok.fangcan.interfaces import Ihouse,Isecondhandfolder,Irenthousefolder,Inewhousefolder

class Ifangcantotalportlet(IPortletDataProvider):
    """A portlet

    It inherits from IPortletDataProvider because for this portlet, the
    data that is being rendered and the portlet assignment itself are the
    same.
    """

    base = schema.Int(title=_(u"base"),
                       description=_(u"Specify the base number of house."),
                       default = 12,
                       required=False)                             

class Assignment(base.Assignment):
    """Portlet assignment.

    This is what is actually managed through the portlets UI and associated
    with columns.
    """

    implements(Ifangcantotalportlet)

    # TODO: Set default values for the configurable parameters here

    base = 12

    # TODO: Add keyword parameters for configurable parameters here
    def __init__(self, base=12):
        self.base = base



    @property
    def title(self):
        """This property is used to give the title of the portlet in the
        "manage portlets" screen.
        """
        return _(u"fangcantotalportlet")


class Renderer(base.Renderer):
    """Portlet renderer.

    This is registered in configure.zcml. The referenced page template is
    rendered, and the implicit variable 'view' will refer to an instance
    of this class. Other methods can be added and referenced in the template.
    """

    render = ViewPageTemplateFile('fangcantotalportlet.pt')
    @memoize
    def newhousenum(self):
#        import pdb
#        pdb.set_trace()
        context = aq_inner(self.context)
        catalog = getToolByName(context, 'portal_catalog')  
        brains = catalog(object_provides=Ihouse.__identifier__,
                         category=u"0")
        return len(brains)
    

    def secondLink(self):
        return self.getLink(Isecondhandfolder) 
    
    def newsLink(self):
        return self.getLink(Inewhousefolder)
    
    def rentLink(self):
        return self.getLink(Irenthousefolder)       
    
    def getLink(self,interface):
        context = aq_inner(self.context)
        catalog = getToolByName(context, 'portal_catalog')  
        brains = catalog(object_provides=interface.__identifier__)
        if len(brains) > 0:
            return brains[0].getURL()
        
    @memoize
    def secondhousenum(self):
#        import pdb
#        pdb.set_trace()
        context = aq_inner(self.context)
        catalog = getToolByName(context, 'portal_catalog')  
        brains = catalog(object_provides=Ihouse.__identifier__,
                         category=u"1")
        return len(brains)
    
    @memoize
    def renthousenum(self):
#        import pdb
#        pdb.set_trace()
        context = aq_inner(self.context)
        catalog = getToolByName(context, 'portal_catalog')  
        brains = catalog(object_provides=Ihouse.__identifier__,
                         category=u"2")
        return len(brains)
    
    def newtotal(self):
        total = self.data.base + self.newhousenum() 
        return total
    def secondtotal(self):
        total = self.data.base + self.secondhousenum() 
        return total    
    def renttotal(self):
        total = self.data.base + self.renthousenum() 
        return total

# NOTE: If this portlet does not have any configurable parameters, you can
# inherit from NullAddForm and remove the form_fields variable.

class AddForm(base.AddForm):
    """Portlet add form.

    This is registered in configure.zcml. The form_fields variable tells
    zope.formlib which fields to display. The create() method actually
    constructs the assignment that is being added.
    """
    form_fields = form.Fields(Ifangcantotalportlet)

    def create(self, data):
        return Assignment(**data)


# NOTE: IF this portlet does not have any configurable parameters, you can
# remove this class definition and delete the editview attribute from the
# <plone:portlet /> registration in configure.zcml

class EditForm(base.EditForm):
    """Portlet edit form.

    This is registered with configure.zcml. The form_fields variable tells
    zope.formlib which fields to display.
    """
    form_fields = form.Fields(Ifangcantotalportlet)
